﻿Imports System
Imports SlimDX
Imports SlimDX.Direct3D11
Imports SlimDX.Direct3D9
Imports System.Runtime.InteropServices

Namespace Engine
    Public Class EntityManager

#Region "Members"
        Private entityList As ArrayList

#End Region
#Region " Methods"""

        

        Public Sub New()
            entityList = New ArrayList()
        End Sub

        Public Sub Add(my_entity As Entity)
            entityList.Add(my_entity)
        End Sub

        Public Sub Update(matView As Matrix, matProj As Matrix)

            For Each e As Entity In entityList
                e.Update(matView, matProj)
            Next

        End Sub
        Public Sub Render(ByRef mdevice As Direct3D9.Device)
            For Each e As Entity In entityList
                e.Render(mdevice)
            Next
        End Sub

        Public Sub Dispose()
            For Each e As Entity In entityList
                e.dispose()
            Next
            entityList = Nothing
        End Sub
#End Region

    End Class

    Public MustInherit Class Entity

#Region "Members"
        Public meffect As SlimDX.Direct3D9.Effect
        Public matWorld As Matrix
        Public matView As Matrix
        Public matProj As Matrix
        Public mesh As Mesh
        Public texture As Texture
        Public material As Material




#End Region
#Region "Methods"

  

        Public Sub New()

        End Sub


        Public Sub LoadObject(ByRef device As Direct3D9.Device, meshFilename As String, effectFilename As String)

            ' load the mesh
            mesh = mesh.FromFile(device, meshFilename, MeshFlags.Managed)

            ' Vertex elements that describe the new format of the mesh
            Dim elements() As VertexElement = {New VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), _
                New VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0), _
                New VertexElement(0, 24, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0), _
                New VertexElement(0, 36, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Tangent, 0), _
                New VertexElement(0, 48, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Binormal, 0), _
                VertexElement.VertexDeclarationEnd}


            Dim tempMesh As Mesh = mesh.Clone(device, MeshFlags.Managed, elements)
            mesh.Dispose()
            mesh = tempMesh

            ' Performs tangent frame computations on a mesh. Tangent, binormal, and optionally normal vectors are generated. 
            ' Singularities are handled as required by grouping edges and splitting vertices.
            '
            mesh.ComputeTangentFrame(TangentOptions.None)

            ' load the effect
            meffect = Direct3D9.Effect.FromFile(device, effectFilename, Nothing, Nothing, ShaderFlags.Debug Or ShaderFlags.PartialPrecision, Nothing)
        End Sub

        Public MustOverride Sub Update(matView As Matrix, matProj As Matrix)
        Public MustOverride Sub Render(ByRef device As Direct3D9.Device)
        Public MustOverride Sub Init(ByRef device As Direct3D9.Device)

        Public Sub dispose()
            If mesh IsNot Nothing Then mesh.Dispose()
            If texture IsNot Nothing Then texture.Dispose()
            If meffect IsNot Nothing Then meffect.Dispose()
        End Sub

#End Region
    End Class


End Namespace

